﻿using SelfManagement.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SelfManagement.Database;
using Ninject;
using SelfManagement.Database.Common;
using SelfManagement.Common.Log;
using SelfManagement.Utility.Extensions;
using SelfManagement.Data;

namespace SelfManagement.Services
{
    public class BaseService<T> : IBaseService<T> where T : EntityBase, new()
    {
        [Inject]
        public IBaseRepository<T> Repository { get; set; }

        [Inject]
        public ILogger Logger { get; set; }

        #region 数据操作

        /// <summary>
        /// 删除
        /// </summary>
        public virtual bool Delete(T obj, bool saveChange = true)
        {
            return Repository.Delete(obj, saveChange);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual bool DeleteBatch(Expression<Func<T, bool>> predicate)
        {
            return Repository.DeleteBatch(predicate);
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        public virtual T Get(Expression<Func<T, bool>> predicate)
        {
            return Repository.Get(predicate);
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        public virtual T Get(string id)
        {
            return Repository.Get(t => t.Id == id);
        }

        /// <summary>
        /// 新增一条记录
        /// </summary>
        public virtual bool Insert(T obj, bool saveChange = true)
        {
            return Repository.Insert(obj, saveChange);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        public virtual bool InsertBatch(IEnumerable<T> objs)
        {
            return Repository.InsertBatch(objs);
        }

        /// <summary>
        /// 创建一个新记录
        /// </summary>
        public virtual T New()
        {
            return Repository.New();
        }

        /// <查询>
        /// 创建一个新记录
        /// </summary>
        public virtual IQueryable<T> Query(Expression<Func<T, bool>> predicate = null)
        {
            return Repository.Query(predicate);
        }

        /// <查询>
        /// 更新
        /// </summary>
        public virtual bool Update(T obj, Func<T, object[]> getKeys = null, bool saveChange = true, bool updateTime = true)
        {
            return Repository.Update(obj, getKeys, saveChange, updateTime);
        }

        /// <查询>
        /// 批量更新
        /// </summary>
        public virtual bool UpdateBatch(Expression<Func<T, bool>> predicate, Func<T, T> setter)
        {
            return Repository.UpdateBatch(predicate, setter);
        }

        /// <查询>
        /// 批量更新
        /// </summary>
        public virtual bool SaveChanges()
        {
            return Repository.SaveChanges();
        }

        #endregion

        #region 数据缓存

        /// <summary>
        /// 获取数据，并缓存全表
        /// </summary>
        /// <param name="refresh">强制刷新缓存</param>
        /// <returns>全表数据</returns>
        public virtual List<T> CacheFullTable(bool refresh = false)
        {
            string key = typeof(T).Name + "Table";

            return SiteCache.GetCache(key, () =>
            {
                return Repository.Query().ToList();
            }, new TimeSpan(24, 0, 0), refresh: refresh);
        }

        #endregion
    }
}
